//
//  DownloadData.m
//  YuanCheng
//
//  Created by dongshangtong on 16/3/14.
//  Copyright © 2016年 dongshangtong. All rights reserved.
//

#import "DownloadData.h"
#import "NSDate+YCExtension.h"

@interface DownloadData();

@end


@implementation DownloadData


/**
 *  get 请求
 *
 *  @param url  请求路径
 *  @param params  请求参数
 *  @param success  成功
 *  @param failure 请求错误
 */
+(void)get:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
        mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    mgr.requestSerializer = [AFJSONRequestSerializer serializerWithWritingOptions:NSJSONWritingPrettyPrinted];
//    mgr.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"applicaton/json",@"text/html",@"text/json",@"text/javascript",@"text/plain",nil];
    
    //    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    [mgr GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (success) {
            success(responseObject);
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
        
        //        [[UIApplication sharedApplication].keyWindow  presentFailureTips:@"网络请求失败!!"];
        
        NSLog(@"网络请求有错误:%@",error);
    }];
    
}

/**
 *  post 请求
 *
 *  @param url  请求路径
 *  @param params  请求参数
 *  @param success  成功
 *  @param failure 请求错误
 */
+(void)post:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"applicaton/json",@"text/html",@"text/json",@"text/javascript",@"text/plain",nil];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    mgr.requestSerializer = [AFJSONRequestSerializer serializer];
    mgr.requestSerializer.timeoutInterval = 8;
    [mgr POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (success) {
            success([NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil]);
        }
        
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
        
        //        [[UIApplication sharedApplication].keyWindow  presentFailureTips:@"网络请求失败!!"];
        NSLog(@"网络请求有错误:%@",error);
        
    }];
    
}


+ (void)getAccess_token{
    
    //保存获取开始获取token
    [NSDate saveCurrentTime];
    NSString *url = [YCUrlManger sharedYCUrlManger].tokenUrl;
    [self get11:url params:nil success:^(id json) {
        
        NSString *token = [[NSString alloc]initWithData:json encoding:NSUTF8StringEncoding];
        NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
        if (token) {
            [def setValue:token forKey:access_token];
            [def synchronize];
        } 
        
    } failure:^(NSError *error) {
        
    }];
    
}


/**
 *  get 请求
 *
 *  @param url  请求路径
 *  @param params  请求参数
 *  @param success  成功
 *  @param failure 请求错误
 */

+(void)get11:(NSString *)url params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    
    
    //    AFHTTPSessionManager *mgr = [DownloadData sharedHttpSessionManager];
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"applicaton/json",@"text/html",@"text/json",@"text/javascript",nil];
    
    [mgr GET:url parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        if (success) {
            success(responseObject);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        if (failure) {
            failure(error);
        }
        [[UIApplication sharedApplication].keyWindow presentFailureTips:@"网络请求失败!!"];
        
    }];
    
}

/**
 *  post 请求
 *
 *  @param url  请求路径
 *  @param params  请求参数
 *  @param success  成功
 *  @param error 请求错误
 */
+(void)post:(NSString *)url SerializerType:(YCRequestSerializerType)type params:(NSDictionary *)params success:(void (^)(id json))success failure:(void (^)(NSError *error))failure
{
    
    AFHTTPSessionManager *mgr = [AFHTTPSessionManager manager];
    
    switch (type) {
        case YCRequestSerializerTypeForm:
            mgr.requestSerializer = [AFHTTPRequestSerializer serializer];
            break;
        case YCRequestSerializerTypeJSON:
            mgr.requestSerializer = [AFJSONRequestSerializer serializerWithWritingOptions:NSJSONWritingPrettyPrinted];
            break;
        case YCRequestSerializerTypePropertyList:
            mgr.requestSerializer = [AFHTTPRequestSerializer serializer];
        default:
            break;
    }
    mgr.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    mgr.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"applicaton/json",@"text/html",@"text/json",@"text/javascript",@"text/plain",nil];
    [mgr POST:url parameters:params progress:^(NSProgress * _Nonnull uploadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        
        if (success) {
            success(responseObject);
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        if (failure) {
            failure(error);
        }
        [[UIApplication sharedApplication].keyWindow presentFailureTips:@"网络请求失败"];
        
    }];
    
    
    
}


@end
